/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import accessor.Customer;
import accessor.CustomerAccessor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tools.EmailValidator;

/**
 *
 * @author QUYET
 */
@WebServlet(name = "add_customer", urlPatterns = {"/add_customer"})
public class add_customer extends BaseServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        EmailValidator validator = new EmailValidator();
        CustomerAccessor accessor = new CustomerAccessor();
        try {
            if (request.getParameter("register") == null) {
                request.getSession().removeAttribute("add_cus_first_name");
                request.getSession().removeAttribute("add_cus_last_name");
                request.getSession().removeAttribute("add_cus_email");
                request.getSession().removeAttribute("add_cus_company");
                request.getSession().removeAttribute("add_cus_address");
                request.getSession().removeAttribute("add_cus_phone");
                request.getSession().removeAttribute("val_first_name");
                request.getSession().removeAttribute("val_last_name");
                request.getSession().removeAttribute("val_email");
                request.getSession().removeAttribute("val_company");
                request.getSession().removeAttribute("val_address");
                request.getSession().removeAttribute("val_phone");
                response.sendRedirect("admin/add_customer.jsp");
            } else {
                String firstName = request.getParameter("first_name");
                String lastName = request.getParameter("last_name");
                String email = request.getParameter("email");
                String company = request.getParameter("company");
                String address = request.getParameter("address");
                String phone = request.getParameter("phone");
                if (!email.equals("")
                        && !firstName.equals("")
                        && !lastName.equals("")
                        && !phone.equals("")
                        && !address.equals("")
                        && validator.validate(email)
                        && !accessor.isEmailExist(email)) {
                    Customer customer = new Customer();
                    customer.setEmail(email);
                    customer.setPassword("1234567");
                    int customerID = accessor.addCustomer(customer);
                    if(customerID>0){
                        customer.setCustomerID(customerID);
                        customer.setFirstName(firstName);
                        customer.setLastName(lastName);
                        customer.setCompany(company);
                        customer.setAddress(address);
                        customer.setPhone(phone);
                        accessor.addCustomerProfile(customer);
                        request.getSession().setAttribute("mess_customer", "<p>Add customer successful!</p>");
                        response.sendRedirect("admin/list_customer.jsp");
                    }
                } else {
                    if(accessor.isEmailExist(email)){
                        request.getSession().setAttribute("add_cus_email","<p>The email already registered!</p>");
                    }
                    if (email.equals("")) {
                        request.getSession().setAttribute("add_cus_email","<p>You must enter your email address!</p>");
                    } else {
                        if (!validator.validate(email)) {
                            request.getSession().setAttribute("add_cus_email","<p>You must enter a valid email address!</p>");
                        }
                    }
                    if (firstName.equals("")) {
                        request.getSession().setAttribute("add_cus_first_name","<p>You must enter your first name!</p>");
                    }
                    if (lastName.equals("")) {
                        request.getSession().setAttribute("add_cus_last_name","<p>You must enter your last name!</p>");
                    }
                    if (phone.equals("")) {
                        request.getSession().setAttribute("add_cus_phone","<p>You must enter your phone number!</p>");
                    }
                    if (address.equals("")) {
                        request.getSession().setAttribute("add_cus_address","<p>You must enter your address!</p>");
                    }
                    request.getSession().setAttribute("val_email", email);
                    request.getSession().setAttribute("val_first_name", firstName);
                    request.getSession().setAttribute("val_last_name", lastName);
                    request.getSession().setAttribute("val_company", company);
                    request.getSession().setAttribute("val_address", phone);
                    request.getSession().setAttribute("val_phone", address);
                    response.sendRedirect("admin/add_customer.jsp");
                }
            }
        } finally {
            out.close();
        }
    }

    protected List<String> validateForm() {
        List<String> error = null;

        return error;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
